#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>


int main(int argc, char *argv[])
{
    int fd;    
    char buf[16];
    u_int32_t recv_cnt = 0;
    
    if (argc < 2)
    {
        printf("%d\r\n", argc);
        printf("plase input paraments\r\n");
        return -1;
    }

    fd = open(argv[1], O_RDWR, 0666);
    if (fd < 0)
    {
        
        printf(" open %s is error: %s\r\n", argv[1], strerror(errno));
        return -1;
    }
    printf("open %s is ok\r\n", argv[1]);

    if (argc > 2)
    { 
        if (! strcmp(argv[2], "read"))
        {
            memset(buf, 0x00, sizeof(buf));
            recv_cnt = read(fd, buf, sizeof(buf));
            if (recv_cnt < 0)
            {
                printf(" read %s is error: %s\r\n", argv[1], strerror(errno));
                return -1;
            }
            printf("read[%d] : %s\n", strlen(buf), buf);

        } else if (! strcmp(argv[2], "write"))
        {
            if (argc > 3)
            {
                // strncpy(buf, argv[3], strlen(argv[3]));
                write(fd, argv[3], strlen(argv[3]));
            }
            else {
                write(fd, "nihao!!", sizeof("nihao!!"));
            }
            
        }
    }

    close(fd);

    return 0;
}
